﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.XPath;
using System.Runtime.Serialization;
using TheOutfield.UmbExt.PrivateMessaging.Model;
using TheOutfield.UmbExt.PrivateMessaging.Data;
using TheOutfield.UmbExt.PrivateMessaging.Util;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using TheOutfield.UmbExt.PrivateMessaging.Services;

namespace TheOutfield.UmbExt.PrivateMessaging.Xslt
{
    public class Library
    {
        protected static PrivateMessageService _privateMessageService;

        static Library()
        {
            _privateMessageService = new PrivateMessageService();
        }

        public static XPathNavigator GetPrivateMessages(string folder)
        {
            var result = new Result();

            try
            {
                result.PrivateMessages = _privateMessageService.GetPrivateMessages((MessageFolder)Enum.Parse(typeof(MessageFolder), folder)).ToList();
            }
            catch (Exception e)
            {
                result.Error = e.Message;
            }

            return result.ToXml();
        }

        public static XPathNavigator GetPrivateMessage(int messageId)
        {
            var result = new Result();

            try
            {
                result.PrivateMessages.Add(_privateMessageService.GetPrivateMessage(messageId));
            }
            catch (Exception e)
            {
                result.Error = e.Message;
            }

            return result.ToXml();
        }

        public static XPathNavigator SendPrivateMessage(string to, string subject, string body)
        {
            var result = new Result();

            try
            {
                _privateMessageService.SendPrivateMessage(to.Split(',', ';'), subject, body);
            }
            catch (Exception e)
            {
                result.Error = e.Message;
            }

            return result.ToXml();            
        }

        public static XPathNavigator UpdatePrivateMessageStatus(int messageId, string status)
        {
            var result = new Result();

            try
            {
                _privateMessageService.UpdatePrivateMessageStatus(messageId, (MessageStatus)Enum.Parse(typeof(MessageStatus), status));
            }
            catch (Exception e)
            {
                result.Error = e.Message;
            }

            return result.ToXml(); 
        }

        public static XPathNavigator UpdatePrivateMessageFolder(int messageId, string folder)
        {
            var result = new Result();

            try
            {
                _privateMessageService.UpdatePrivateMessageFolder(messageId, (MessageFolder)Enum.Parse(typeof(MessageFolder), folder));
            }
            catch (Exception e)
            {
                result.Error = e.Message;
            }

            return result.ToXml(); 
        }

        public static XPathNavigator DeletePrivateMessage(int messageId)
        {
            var result = new Result();

            try
            {
                _privateMessageService.DeletePrivateMessage(messageId);
            }
            catch (Exception e)
            {
                result.Error = e.Message;
            }

            return result.ToXml(); 
        }
    }

    [DataContract]
    public class Result
    {
        [DataMember]
        public List<PrivateMessage> PrivateMessages { get; set; }

        [DataMember]
        public string Error { get; set; }

        public Result()
        {
            PrivateMessages = new List<PrivateMessage>();
        }
    }
}